﻿@model ViewPageModel
@{
    Layout = null;
    var route = Url.CreateCurrentRoute();
    if(Model.PageIndex <= 0) {
        string pageIndexString = Url.GetQueryString("page");
        int pageIndex;
        if(!Int32.TryParse(pageIndexString, out pageIndex)) {
            pageIndex = 1;
        }
        Model.PageIndex = pageIndex;
    }
    if(Model.PageSize <= 0) {
        string pageSizeString = Url.GetQueryString("pagesize");
        int pageSize;
        if(!Int32.TryParse(pageSizeString, out pageSize)) {
            pageSize = 20;
        }
        Model.PageSize = pageSize;
    }
    int currentStartPage, currentEndPage;
    currentStartPage = Model.PageIndex - Model.ShowPageCount / 2;
    currentEndPage = Model.PageIndex + Model.ShowPageCount / 2;
    if (currentStartPage < 1) {
        currentEndPage = currentEndPage - currentStartPage + 1;
        currentStartPage = 1;
    }
    if (currentEndPage > Model.PageCount) {
        int moreRecord = currentEndPage - Model.PageCount;
        currentStartPage = currentStartPage - moreRecord < 1 ? 1 : currentStartPage - moreRecord;
        currentEndPage = Model.PageCount;
    }
}

<div class="pageContainer">
@if (Model.PageIndex > 1) {
<a href="@Url.CreatePageUrl(route, 1)">首页</a>
}
@if (Model.PageIndex > 2) {
<a href="@Url.CreatePageUrl(route, Model.PageIndex - 1)">上页</a>
}
@for (int i = currentStartPage; i <= currentEndPage; i++) {
    if (i == Model.PageIndex) {
        <strong>@i</strong>
    } else {
        <a href="@Url.CreatePageUrl(route, i)">@i</a>
    }
}
@if (Model.PageIndex < Model.PageCount - 1) {
    <a href="@Url.CreatePageUrl(route, Model.PageIndex + 1)">下页</a>
}
@if (Model.PageIndex < Model.PageCount) {
    <a href="@Url.CreatePageUrl(route, Model.PageCount)">末页</a>
}
<span>@Model.RecordCount @Model.PageIndex/@Model.PageCount</span>
</div>